//
//  MainController.swift
//  SCA
//
//  Created by tjun on 2025/8/31.
//

import UIKit


class MainController: UITabBarController, UITabBarControllerDelegate {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.isTranslucent = false
        self.delegate = self

        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground() // 不透明背景
        appearance.backgroundColor = Assets.Color.tabbar_bg       // 背景色
        appearance.shadowColor = Assets.Color.tabbar_separator   // 想隐藏就设置为 .clear
        
        appearance.stackedLayoutAppearance.normal.iconColor = Assets.Color.tabbar_title
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
            .foregroundColor: Assets.Color.tabbar_title
        ]

        appearance.stackedLayoutAppearance.selected.iconColor = Assets.Color.tabbar_selectTitle
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
            .foregroundColor: Assets.Color.tabbar_selectTitle
        ]

        tabBar.standardAppearance = appearance
        if #available(iOS 15.0, *) {
            tabBar.scrollEdgeAppearance = appearance
        }
        
//        if #available(iOS 15.0, *) {
//            let appearance = UITabBarAppearance()
//            appearance.configureWithOpaqueBackground()
//            appearance.backgroundColor = .blue
//            
//            // 修改分割线颜色
//            appearance.shadowColor = .red  // 想隐藏就设置为 .clear
//            
//            tabBar.standardAppearance = appearance
//            tabBar.scrollEdgeAppearance = appearance
//        }
//        
//
        addChildViewController()
    }
    
    private lazy var tabItemConfigurations: [[String: Any]] = {
        return [
            [
                "controller": HomeController(),
                "iconName": "tabBar_home",
                "selectIconName": "tabBar_home_select",
                "title": "首页".localizedString,
            ],
            [
                "controller": ComputingPowerController(),
                "iconName": "tabbar_computingPower",
                "selectIconName": "tabbar_computingPower_select",
                "title": "算力".localizedString,
            ],
            [
                "controller": NewsController(),
                "iconName": "tabBar_news",
                "selectIconName": "tabBar_news_select",
                "title": "资讯".localizedString,
            ],
            [
                "controller": WalletController(),
                "iconName": "tabBar_wallet",
                "selectIconName": "tabBar_wallet_select",
                "title": "钱包".localizedString,
            ]
        ]
    }()
    
    func addChildViewController() {
        for obj in tabItemConfigurations {
            if let controller = obj["controller"] as? UIViewController,
               let image = obj["iconName"] as? String,
               let selectImage = obj["selectIconName"] as? String,
               let title = obj["title"] as? String {
                addChildViewController(childController: controller, image: image, selectImage: selectImage, title: title)
            }
        }
    }
    
    func addChildViewController(childController: UIViewController, image: String, selectImage: String, title: String) {
        let navigationController = NavigationController(rootViewController: childController)
        navigationController.view.backgroundColor = .white
        addChild(navigationController)
        childController.title = title
        childController.navigationItem.title = title
        childController.tabBarItem.image = UIImage(named: image)
        childController.tabBarItem.selectedImage = UIImage(named: selectImage)
    }
    
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        UIView.setAnimationsEnabled(false)
//        printf("setAnimationsEnabled-false")
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
            UIView.setAnimationsEnabled(true)
//            printf("setAnimationsEnabled-true")
        })
    }
 
    
 
}

